var urlList = server_host + '/mainview/demo/getBrandShopList.do';

var brandId = getParameterByName('brandId');

if (brandId == '') {
	brandId = sessionStorage.getItem('brandId');
}
//是否订阅号，商户公众号入口判断，并存入session
sessionStorage.setItem('isSubscribe', getParameterByName('isSubscribe') != '');

var appModule = angular.module('CheerHiWx', []);
appModule.controller('IndexCtrl', ['$scope', '$http', function($scope, $http) {

	$scope.pageIndex = 1;

	$scope.listArr = [];

	$scope.loadMoreTxt = {
		code: 0,
		dscp: '加载更多'
	};

	//加载列表
	$scope.loadList = function() {
		var promise = $http.post(urlList + '?brandId=' + brandId + '&lat=30.283088&lng=120.131400' + '&page=' + $scope.pageIndex).then(function(response) {
			return response.data;
		});
		promise.then(function(data) {
			console.log(data);
			if (data.shopList.length != 0)
				$scope.listArr = $scope.listArr.concat(data.shopList);
			else
				$scope.loadMoreTxt = {
					code: 1,
					dscp: '到底儿了~'
				}
			if ($scope.pageIndex == 1) {
				$scope.brand = data.brand;
				sessionStorage.setItem('brandId', brandId);
			}
		});
	}

	//点击选择店铺跳转
	$scope.shopSelect = function(shopId, shopName, foodInfo) {
		//将当前店铺id存入session
		sessionStorage.setItem('shopId', shopId);
		sessionStorage.setItem('shopName', shopName);
		location.href = 'details.html?shopId=' + shopId + '&foodInfo=' + encodeURIComponent(foodInfo);
	}

	//点击选择店铺跳转到点餐页面
	$scope.shopSelectMeal = function(shopId, shopName) {
		//将当前店铺id存入session
		sessionStorage.setItem('shopId', shopId);
		sessionStorage.setItem('shopName', shopName);
		location.href = 'order-foodinfo.html';
	}

	//加载更多
	$scope.loadMore = function() {
		if ($scope.loadMoreTxt.code == 0) {
			$scope.pageIndex++;
			$scope.loadList();
		}
	}

	$scope.calcStarWidth = function(score) {
		var percent = score / 5;
		var totalLength = 64;
		var starLength = totalLength - (2 / 2) * 4;
		if (score > 0 && score <= 1) {
			return starLength * percent;
		} else if (score > 1 && score <= 2) {
			return starLength * percent + 1;
		} else if (score > 2 && score <= 3) {
			return starLength * percent + 2;
		} else if (score > 3 && score <= 4) {
			return starLength * percent + 3;
		} else if (score > 4 && score <= 5) {
			return starLength * percent + 4;
		}
	}

	$scope.loadList();
}]);

//自动登录
autoLogin();